package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemIndexFramilyAddBinding;
import com.chuanjiabao.wisdom.listener.OnAddFamilyUserListener;
import com.chuanjiabao.wisdom.listener.OnFamilyTreeClickListener;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;

import java.util.List;

/**
 * Created by 86178 on 2019/8/2.
 * author mogen
 * 用心，成就一切。
 */

public class IndexFamilyAddAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {

    private List<FamilyDeatilCallbackBean.DataBean> strings;
    private Context mcontext;
    public OnAddFamilyUserListener listener;
    IndexFamilyUsersAdapter adapter;
    public OnFamilyTreeClickListener onFamilyTreeClickListener;
    LinearLayoutManager linearLayoutManager;

    public void setOnFamilyTreeClickListener(OnFamilyTreeClickListener onFamilyTreeClickListener){
        this.onFamilyTreeClickListener = onFamilyTreeClickListener;
    }

    public void setOnAddFamilyUserListener(OnAddFamilyUserListener listener) {
        this.listener = listener;
    }

    public IndexFamilyAddAdapter(Context context,List<FamilyDeatilCallbackBean.DataBean> strings){
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemIndexFramilyAddBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_index_framily_add, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        tv.tvModuleAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.setOnAddFamilyUserListener(position);
                }
            }
        });

        adapter = new IndexFamilyUsersAdapter(mcontext,strings.get(position).getMembers());
        //        TaoLinear taoLinear = new TaoLinear(mcontext,3);
        //        taoLinear.setScrollEnabled(true);
        linearLayoutManager = new LinearLayoutManager(mcontext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        tv.recycler.setLayoutManager(linearLayoutManager);
        tv.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                if (onFamilyTreeClickListener != null) {
                    onFamilyTreeClickListener.setOnFamilyTreeClickListener(position, pos);
                }
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyDeatilCallbackBean.DataBean> {
        ItemIndexFramilyAddBinding binding;
        ImageView tvModuleAdd;
        RecyclerView recycler;

        public TourViewHolder(ItemIndexFramilyAddBinding dataBinding) {
            super(dataBinding.getRoot());
            binding = dataBinding;
            tvModuleAdd = dataBinding.tvModuleAdd;
            recycler = dataBinding.recycler;
        }

        @Override
        public void bindData(FamilyDeatilCallbackBean.DataBean data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }
}